package cn.fczds.camhelper.order.Service.impl;

import cn.fczds.camhelper.comment.mapper.commentMapper;
import cn.fczds.camhelper.comment.pojo.dto.commentSaveParam;
import cn.fczds.camhelper.order.Service.OrderService;
import cn.fczds.camhelper.order.mapper.OrderMapper;
import cn.fczds.camhelper.order.pojo.dto.OrderQuery;
import cn.fczds.camhelper.order.pojo.dto.OrderSaveParam;
import cn.fczds.camhelper.order.pojo.dto.OrderStatus;
import cn.fczds.camhelper.order.pojo.entity.Order;
import cn.fczds.camhelper.order.pojo.vo.OrderVO;
import cn.fczds.camhelper.user.mapper.UserMapper;
import cn.fczds.camhelper.user.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    commentMapper commentMapper;
    @Override
    public List<OrderVO> select(OrderQuery orderQuery) {
        List<OrderVO> list = orderMapper.selectOrder(orderQuery);
        for(int i = 0;i<list.size();i++)
        {
            Long userId = list.get(i).getUserId();
            Long riderId = list.get(i).getRiderId();
            if(userId != null)
            {
                UserVO user1 = userMapper.selectById(userId);
                String name1 = user1.getNickname();
                list.get(i).setUserName(name1);
            }
            if(riderId != null){
                UserVO user2 = userMapper.selectById(riderId);
                String name2 = user2.getNickname();
                list.get(i).setRiderName(name2);
            }
        }
        log.debug("list:{}",list);
        return list;
    }

    @Override
    public void insert(OrderSaveParam orderSaveParam) {
        Order order = new Order();
        BeanUtils.copyProperties(orderSaveParam, order);
        log.debug("order:{}", order);
        if(order.getOrderStatus().equals("待接单")) {
            order.setCreationTime(new Date());
            orderMapper.insert(order);
        }else if(order.getOrderStatus().equals("进行中")){
            order.setAcceptTime(new Date());
            orderMapper.update(order);
        }else if(order.getOrderStatus().equals("待评价")){
            order.setEndTime(new Date());
            orderMapper.update(order);
        }else{
            /*System.out.println(orderSaveParam.getCommentImgUrl());
            String[] list = orderSaveParam.getCommentImgUrl().split(",");
            System.out.println("listLength:"+list.length);
            if(list.length>0){
                for(int i = 0;i<list.length;i++){
                    commentSaveParam commentSaveParam = new commentSaveParam();;
                    commentSaveParam.setOrderId(order.getOrderId());
                    commentSaveParam.setCommentImgUrl(list[i]);
                    commentMapper.insert(commentSaveParam);
                }
            }*/
            orderMapper.update(order);
        }
    }

    @Override
    public List<OrderVO> selectAll(OrderQuery orderQuery) {
        List<OrderVO> list = orderMapper.selectAll(orderQuery);
        for(int i = 0;i<list.size();i++)
        {
            Long userId = list.get(i).getUserId();
            Long riderId = list.get(i).getRiderId();
            if(userId != null)
            {
                UserVO user1 = userMapper.selectById(userId);
                String name1 = user1.getNickname();
                list.get(i).setUserName(name1);
            }
            if(riderId != null){
                UserVO user2 = userMapper.selectById(riderId);
                String name2 = user2.getNickname();
                list.get(i).setRiderName(name2);
            }
            System.out.println(list.get(i));
        }
        log.debug("list:{}",list);
        return list;
    }

    @Override
    public List<OrderVO> selectStatus(OrderStatus orderStatus) {
        List<OrderVO> list = orderMapper.selectStatus(orderStatus);
        return list;
    }

}
